<template>
  <div>
    <div v-for="(item,index) in taskFormConfigs">
      <div>
        <div>
          <el-divider content-position="left">{{ item.taskName }}</el-divider>
        </div>
        <proc-task-form
            :ref="el=>initActTaskFormRefs(el,item)"
            :formConfig="item.taskFormConfig"
            :form-model="item.formData"
            :is-detail="item.isDetail"
            @submit="submitForm"></proc-task-form>
      </div>
    </div>

  </div>

</template>

<script>

import procTaskForm from "./procTaskForm.vue";

import {ref} from "vue";
import startActTools from "@/views/start/activiti/components/startActTools";

const actTaskFormRefs = ref([])
export default {
  name: "startActTaskFrom",
  components: {procTaskForm},
  props: {
    modelValue: Array,
    formBtn: {
      type: Boolean,
      default: true,
    },
  },
  computed: {
    taskFormConfigs() {
      if (!this.modelValue) {
        return [];
      }
      if (this.modelValue.length < 1) return;
      let temp = []
      for (let i = 0; i < this.procTasks[i].length; i++) {
        let item = this.procTasks[i]
        if (!item.taskFormConfig) {
          continue
        }
        let node = {}
        node.formData = item.nodeFormData ||= {}
        node.taskFormConfig = item.taskFormConfig
        node.taskName = item.taskName
        node.taskInstId = item.taskInstId
        node.isDetail = true
        node.isAuthIng = item.handlerState === 10
        node.isDetail = !node.isAuthIng
        temp.push(node)
      }
      return temp;
    }
  },
  data() {
    return {

      procTasks: [],
      awaitSubmit: {
        formData: null,
        formDone: null,
        nextTaskAuthUser: null,
      },
    }
  },
  created() {

  },
  methods: {

    initActTaskFormRefs(el, item) {
      if (!item || !item.isAuthIng) {
        return
      }
      if (el) {
        actTaskFormRefs.value.push(el);
      }
    },
    submit() {
      this.startValidateForm()
    },
    startValidateForm() {
      if (actTaskFormRefs.value.length < 1) {
        return
      }
      let submit = actTaskFormRefs.value[0].submit
      if (!submit) {
        this.$message.error("子组件未定义提交函数：submit")
        return;
      }
      submit();
    },

    resetForm() {
      if (actTaskFormRefs.value.length < 1) {
        return
      }
      let resetForm = actTaskFormRefs.value[0].resetForm
      if (resetForm) {
        resetForm();
        return;
      }
      this.$message.error("子组件未定义重置表单函数：resetForm")
    },
    submitForm(data, done) {
      let formData = {};
      for (const key in data) {
        if (key.startsWith("$")) {
          continue
        }
        formData[key] = data[key];
      }
      this.awaitSubmit.formData = data;
      this.awaitSubmit.formDone = done;

    },
    callSubmitFormEvent() {
      this.$emit("submit", this.awaitSubmit)
    },

  }
}
</script>

<style scoped>

</style>
